package com.example.universita.esami;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import it.mylibretto.app.R;

public class Details extends Activity  {
	
	public static String EXTRA_MESSAGE;
	ArrayAdapter<String> arrayAdapter;
	DBDataUniversity db;
	boolean orderNome=true, orderVoto=true, orderAnno=true, orderCrediti=true, orderSemestre=true;
	int nameCol;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_details);
		arrayAdapter = new ArrayAdapter<String>(this, R.layout.listitem, R.id.listItemTextView);
		db = new DBDataUniversity(this);
		db.open();
		Cursor cursor = db.getExams();
		
		while(cursor.moveToNext()){
			
			arrayAdapter.add(cursor.getString(1));
			
		}
		
		db.close();
		
        ListView listView = (ListView) findViewById(R.id.Lista_esami);
        
		listView.setAdapter(arrayAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
		
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
				
				ListView listView = (ListView) adapterView;
				ArrayAdapter<String> arrayAdapter = (ArrayAdapter<String>) listView.getAdapter();
				String item = arrayAdapter.getItem(position);
				Intent intent = new Intent (getBaseContext(), Dettagli_esame.class);
				String pkg = getPackageName();
				intent.putExtra(pkg+".myString", item);
				startActivity(intent);
				
			}
		});
    
    }
    
    @Override
    public void onResume() {
    	aggiornaElenco();
    	super.onResume();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_details, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	Intent intent;
    	Cursor cursor;
    	ListView listView;
    	
    	switch(item.getItemId()){
    	    case R.id.statistiche: // Vai a Dettagli Esame
 	    	   		intent = new Intent (this, StatisticActivity.class);
 	    	   		startActivity(intent);
 	    	   		break;      
    	    case R.id.carriera: // Vai a Carriera
 	    	   		intent = new Intent(this, Carriera.class);
 	    	   		startActivity(intent);  
 	    	   		break;             
    	    case R.id.home: // Vai alla Home
   	   	   			intent = new Intent(this, MainActivity.class);
   	   	   			startActivity(intent);  
   	   	   			break;  
    	    case R.id.nome:
    	    		db.open();
    	    		arrayAdapter.clear();
    	    		orderNome = !(orderNome);
    	    		if(orderNome)  cursor = db.getExamsOrder("name ASC");
    	    		else   		   cursor = db.getExamsOrder("name DESC");
    			
    	    		while(cursor.moveToNext()){
    	    			nameCol = cursor.getColumnIndex("name");
    	    			arrayAdapter.add(cursor.getString(nameCol));
    	    		}
    			
    	    		db.close();
    	    		
    	    		listView = (ListView) findViewById(R.id.Lista_esami);
    	    		arrayAdapter.notifyDataSetChanged();
    	    		listView.setAdapter(arrayAdapter);  
    	    	 break;
    	    case R.id.voto:
    	    		db.open();
    	    		arrayAdapter.clear();
    	    		if(orderVoto)  cursor = db.getExamsOrder("vote ASC, name ASC");
    	    		else   		   cursor = db.getExamsOrder("vote DESC, name ASC");
    	    		orderVoto = !(orderVoto);
			
    	    		while(cursor.moveToNext()){
    	    			nameCol = cursor.getColumnIndex("name");
    	    			arrayAdapter.add(cursor.getString(nameCol));
    	    		}
			
    	    		db.close();
	    		
    	    		listView = (ListView) findViewById(R.id.Lista_esami);
    	    		arrayAdapter.notifyDataSetChanged();
    	    		listView.setAdapter(arrayAdapter);  
   	    	     break; 
    	    case R.id.anno:
    	    		db.open();
    	    		arrayAdapter.clear();
    	    		if(orderAnno)  cursor = db.getExamsOrder("year ASC, name ASC");
    	    		else   		   cursor = db.getExamsOrder("year DESC, name ASC");
    	    		orderAnno = !(orderAnno);
		
    	    		while(cursor.moveToNext()){
    	    			nameCol = cursor.getColumnIndex("name");
    	    			arrayAdapter.add(cursor.getString(nameCol));
    	    		}
		
    	    		db.close();
    		
    	    		listView = (ListView) findViewById(R.id.Lista_esami);
    	    		arrayAdapter.notifyDataSetChanged();
    	    		listView.setAdapter(arrayAdapter);  
   	    	     break; 
    	    case R.id.semestre:
	    			db.open();
	    			arrayAdapter.clear();
	    			if(orderSemestre)  cursor = db.getExamsOrder("year ASC, semester ASC, name ASC");
	    			else   		   cursor = db.getExamsOrder("year DESC, semester DESC, name ASC");
	    			orderSemestre = !(orderSemestre);
	
	    			while(cursor.moveToNext()){
	    				nameCol = cursor.getColumnIndex("name");
	    				arrayAdapter.add(cursor.getString(nameCol));
	    			}
	
	    			db.close();
		
	    			listView = (ListView) findViewById(R.id.Lista_esami);
	    			arrayAdapter.notifyDataSetChanged();
	    			listView.setAdapter(arrayAdapter);  
	    	     break;         
    	    case R.id.crediti:
    	    		db.open();
    	    		arrayAdapter.clear();
    	    		if(orderCrediti)  cursor = db.getExamsOrder("credits ASC, name ASC");
    	    		else   		   cursor = db.getExamsOrder("credits DESC, name ASC");
    	    		orderCrediti = !(orderCrediti);
		
    	    		while(cursor.moveToNext()){
    	    			nameCol = cursor.getColumnIndex("name");
    	    			arrayAdapter.add(cursor.getString(nameCol));
    	    		}
		
    	    		db.close();
    		
    	    		listView = (ListView) findViewById(R.id.Lista_esami);
    	    		arrayAdapter.notifyDataSetChanged();
    	    		listView.setAdapter(arrayAdapter);  
  	    	     break;      
    	}
     return true;
    }
    
    public void aggiornaElenco(){
    	db.open();
    	arrayAdapter.clear();
		Cursor cursor = db.getExamsOrder("name ASC");
		
		while(cursor.moveToNext()){
			nameCol = cursor.getColumnIndex("name");
			arrayAdapter.add(cursor.getString(nameCol));
			
		}
		arrayAdapter.notifyDataSetChanged();
		db.close();
    }
}
